const { regClass } = Laya;
import { LevelConfigInfo } from "../../../Data/LevelConfig";
import { StaticData } from "../../../Data/StaticData";
import { GameRes } from "../../../GameRes";
import { GameListImgBase } from "./GameListImg.generated";
import { ItemImg } from "./ItemImg";

@regClass()
export class GameListImg extends GameListImgBase {
    /** 状态 */
    type: EState;
    actClick: Function;
    resItemImg: Laya.Prefab;

    arrImgList: ItemImg[];

    onAwake(): void {
        this.resItemImg = GameRes.getResPrefab(GameRes.GameItemImg);

        this.BtnShowHide.on(Laya.Event.CLICK, this, this.onClickShowHide)
    }
    onDestroy(): void {
        this.BtnShowHide.off(Laya.Event.CLICK, this, this.onClickShowHide)
    }
    public init(actClick: Function = undefined) {
        this.actClick = actClick;
        this.setType(EState.hide)
    }

    public setInfo(info: LevelConfigInfo) {
        console.log("GameListImg 设置数据", info);
        if (this.arrImgList == undefined) {
            this.arrImgList = [];
        }

        const maxIndex = this.arrImgList.length > info.arrLevelQuestion.length ? this.arrImgList.length : info.arrLevelQuestion.length;
        for (let i = 0; i < maxIndex; i++) {
            let obj;
            if (i < this.arrImgList.length) {
                obj = this.arrImgList[i];
            } else {
                obj = this.resItemImg.create() as ItemImg;
                this.HBox.addChild(obj);
                this.arrImgList.push(obj);
            }

            if (i < info.arrLevelQuestion.length) {
                obj.setInfo(info.arrLevelQuestion[i].arrQuestionOptions[info.arrLevelQuestion[i].questionAnswer]);
                obj.showItem();
            } else {
                obj.hideItem();
            }
        }

        console.log("当前数据量", info.arrLevelQuestion.length);
        // 最大放入的对象数量
        if (info.arrLevelQuestion.length <= 0) {
            this.HBox.width = 0;
            return;
        }
        const lrLength = 50;
        const space = 24;
        const maxItem = Math.floor((this.width - lrLength * 2) / (this.arrImgList[0].width + space));
        console.log("最大放入的对象数量", maxItem);
        let nowCount = info.arrLevelQuestion.length;
        if (info.arrLevelQuestion.length > maxItem) {
            nowCount = maxItem;
        }

        console.log("size=>", this.arrImgList[0].width);
        this.Panel.width = nowCount * (this.arrImgList[0].width + space) + lrLength * 2;
        this.HBox.width = info.arrLevelQuestion.length * (this.arrImgList[0].width + space) + lrLength * 2;

    }

    private onClickShowHide() {
        if (this.type == EState.hide) {
            this.setType(EState.show);
        } else if (this.type == EState.show) {
            this.setType(EState.hide);
        }
        this.actClick?.(this.type);
    }

    public setType(type: EState) {
        if (type == EState.hide) {
            this.ImgArrow.rotation = 0;
        } else if (type == EState.show) {
            this.ImgArrow.rotation = 180;
        }
        this.type = type;
    }

    public setTexture(id: number, texture: Laya.Texture) {
        if (!this.arrImgList[id]) { return; }
        this.arrImgList[id].setTexture(texture);
    }

}
/** 状态 */
export enum EState {
    hide,
    show
}